#!c:/perl/bin/perl.exe

use DBI;
use DBD::mysql;
use DIVA_DB;
use WWCOM;
use CGI;
use strict;
	
my $DEBUG= 1;
$DIVA_DB::DEBUG= $DEBUG;

$DIVA_DB::DATABASE_NAME= "wwcom";
$DIVA_DB::DATABASE_SERVER= "localhost:3306";

my $cgi= new CGI;
my %outvars;

my @names= <$WWCOM::DATAROOTDIR/images/*.dat.txt>;

my %allstories;
my $cols= ['IMAGE_CAPTION','IMAGE_DESCRIPTION','IMAGE_XSIZE','IMAGE_YSIZE','IMAGE_URL','FK_OBJECT_ID','FK_USER_ID'];
my $vals;

my $dbh= &DIVA_DB::DB_CONNECT('root','FineBob+');
for(my $i= 0; $i<$#names; $i++) {
	print "Image: $names[$i]\n";
	my %imhash= &WWCOM::READFILE($names[$i]);
	$vals= [];
	my $count= 0;
	
	$allstories{$i}= \%imhash;
	$$vals[$count][0]= &DIVA_DB::DECODE($imhash{'caption'});
	my $date= &DIVA_DB::DECODE($imhash{'date'});
	$date=~ /([0-9]+)\.([\d]+)\.([\d]+)/;
	my $db_date= "$3-$2-$1";
	if ($db_date eq '--') {
		$db_date= '0000-00-00';
	}
	print "\t** $date: $db_date\n";
	$$vals[$count][1]= &DIVA_DB::DECODE($imhash{'details'});
	my $size= &DIVA_DB::DECODE($imhash{'size'});
	$size=~ /([0-9]+)\s([0-9]+)/;
	$$vals[$count][2]= $1;
	$$vals[$count][3]= $2;
	$$vals[$count][4]= 'data/'.&DIVA_DB::DECODE($imhash{'filename'});
	
	&DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'OBJECT',['FK_CONTENTTYPE_ID','OBJECT_DATE'],[[4,$db_date]]);
	$$vals[$count][5]= $dbh->{'mysql_insertid'};
	$$vals[$count][6]= 1;	#user is me
	$allstories{$i}{'FK_OBJECT_ID'}= $dbh->{'mysql_insertid'};
	&DIVA_DB::INSERT_DATA($dbh,$cgi,\%outvars,'IMAGE',$cols,$vals);
	$allstories{$i}{'PK_IMAGE_ID'}= $dbh->{'mysql_insertid'};
}
	
	exit(0);
	
	
# now do relationships	  
foreach my $key (keys %allstories) {
	if ($allstories{$key}{'relatedthreadtitles'} ne "") {
		print "Related: $key - $allstories{$key}{'relatedthreads'}\n";
	}
}
	
$dbh->disconnect; 